package com.zr.sparesubclass.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zr.sparesubclass.bean.*;
import com.zr.sparesubclass.mapper.*;
import com.zr.sparesubclass.model.*;
import com.zr.sparesubclass.service.InqueryService;
import com.zr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.bind.SchemaOutputResolver;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;

@Service
public class InqueryServiceImpl implements InqueryService {
    @Autowired
    private InqueryMapper inqueryMapper;
    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ForminlineMapper forminlineMapper;
    @Override
    public ResultVO<AllRecords> search(AddSpareVo addSpareVo) {
        int count = inqueryMapper.queryCount(addSpareVo);
        System.out.print(count);
        List<Spare> spares = inqueryMapper.queryPage(addSpareVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(spares);
        allRecords.setPageIndex(addSpareVo.getPageIndex());
        allRecords.setPageSize(addSpareVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<AllRecords> searchPartnerPage(SearchPartnerPageVo searchPartnerPageVo) {
        int count = partnerMapper.queryCount(searchPartnerPageVo);
        List<Spare> spares = partnerMapper.queryPage(searchPartnerPageVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(spares);
        allRecords.setPageIndex(searchPartnerPageVo.getPageIndex());
        allRecords.setPageSize(searchPartnerPageVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<List<SupplierDetails>> getSupplierDetails(Integer legalPersonId) {
        return ResultVOBuilder.success(supplierMapper.getSupplierDetailsById(legalPersonId));
    }

    @Override
    @Transactional
    public ResultVO<InquiryListAddVo> inquiryListAdd(InquiryListAddVo inquiryListAddVo) {
        List<SpareDetails> list = inquiryListAddVo.getInquiryDetailsList();
        List<SupplierDetails> partners = inquiryListAddVo.getSupplierDetailsList();
        //添加验证
        //供应商备件不能为空
        if(list.size()<=0){
            return ResultVOBuilder.error("500","没有选择备件");
        }
        if(partners.size()<=0){
            return ResultVOBuilder.error("500","没有选择供应商");
        }
        //1.验证备件是否有供应商
        List<String> str= new ArrayList<>();
        for(SupplierDetails p:partners){
           str.add(p.getVendorCode());
        }
        List<String> str1= new ArrayList<>();
        for(SpareDetails s:list){
            str1.add(s.getSpareCoding());
        }
        List<String> partCode = inqueryMapper.checkSpare(list);
        List<String> spareCode = inqueryMapper.checkSupplier(partners);
        str1.removeAll(spareCode);
        if(str1.size()>0){
            return  ResultVOBuilder.error("500",str1+"没有供应商生产");
        }
        //2.验证供应商是否有备件
        str.removeAll(partCode);
            if(str.size()>0){
                return  ResultVOBuilder.error("500","供应商"+str+"没有生产的备件");
            }
        inquiryListAddVo.setCreateTime(new Date());
        inquiryListAddVo.setRequestForQuotationId(UUID.randomUUID().toString());
        supplierMapper.add(inquiryListAddVo);
        inqueryMapper.add(inquiryListAddVo);
        forminlineMapper.add(inquiryListAddVo);
        return ResultVOBuilder.success(inquiryListAddVo);
    }

    @Override
    public ResultVO<AllRecords> inquiryListsearch(InquiryListVo inquiryListVo) {
        List<Forminline> list = forminlineMapper.inquiryListsearch(inquiryListVo);
        for(Forminline f:list){
            f.setInquiryStatusName(InquiryStatusEnum.getLabel(f.getInquiryStatus()));
        }
        int count = forminlineMapper.inquiryListsearchCount(inquiryListVo);
        AllRecords allRecords  = new AllRecords();
        allRecords.setDataList(list);
        allRecords.setPageSize(inquiryListVo.getPageSize());
        allRecords.setPageIndex(inquiryListVo.getPageIndex());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<InquiryListAddVo> queryById(MidifyIdVO midifyIdVO) {
        InquiryListAddVo forminline = forminlineMapper.queryById(midifyIdVO.getRequestForQuotationId());
        forminline.setInquiryDetailsList(inqueryMapper.queryById(midifyIdVO.getRequestForQuotationId()));
        forminline.setSupplierDetailsList(supplierMapper.queryById(midifyIdVO.getRequestForQuotationId()));
        return ResultVOBuilder.success(forminline);
    }

    @Override
    @Transactional
    public ResultVO<InquiryListAddVo> inquiryListUpdate(InquiryListAddVo inquiryListAddVo) {
        //1.修改是：主表修改，子表先删除，再添加
        forminlineMapper.inquiryListUpdate(inquiryListAddVo);
        inqueryMapper.delete(inquiryListAddVo.getRequestForQuotationId());
        supplierMapper.delete(inquiryListAddVo.getRequestForQuotationId());
        List<SpareDetails> list = inquiryListAddVo.getInquiryDetailsList();
        List<SupplierDetails> partners = inquiryListAddVo.getSupplierDetailsList();
        //添加验证
        //供应商备件不能为空
        if(list.size()<=0){
            return ResultVOBuilder.error("500","没有选择备件");
        }
        if(partners.size()<=0){
            return ResultVOBuilder.error("500","没有选择供应商");
        }
        //1.验证备件是否有供应商
        List<String> str= new ArrayList<>();
        for(SupplierDetails p:partners){
            str.add(p.getVendorCode());
        }
        List<String> str1= new ArrayList<>();
        for(SpareDetails s:list){
            str1.add(s.getSpareCoding());
        }
        List<String> partCode = inqueryMapper.checkSpare(list);
        List<String> spareCode = inqueryMapper.checkSupplier(partners);
        str1.removeAll(spareCode);
        if(str1.size()>0){
            return  ResultVOBuilder.error("500",str1+"没有供应商生产");
        }
        //2.验证供应商是否有备件
        str.removeAll(partCode);
        if(str.size()>0){
            return  ResultVOBuilder.error("500","供应商"+str+"没有生产的备件");
        }
        supplierMapper.add(inquiryListAddVo);
        inqueryMapper.add(inquiryListAddVo);
        return ResultVOBuilder.success(inquiryListAddVo);
    }

    @Override
    @Transactional
    public ResultVO<ExamineVo> inquiryAuditorupdate(ExamineVo examineVo) {
        forminlineMapper.inquiryAuditorupdate(examineVo);
        return ResultVOBuilder.success(examineVo);
    }

    @Override
    public ResultVO<ApprovalVo> inquiryApproverupdate(ApprovalVo approvalVo) {
        forminlineMapper.inquiryApproverupdate(approvalVo);
        return ResultVOBuilder.success(approvalVo);
    }

    @Override
    public ResultVO<VoidVo> inquiryListmodifyState(VoidVo voidVo) {
        forminlineMapper.inquiryListmodifyState(voidVo);
        return ResultVOBuilder.success(voidVo);
    }

    @Override
    public ResultVO<AllRecords> quotationListsearchPre(QutationListVo qutationListVo) {
        AllRecords allRecords = new AllRecords();
        List<QuotationListResult>  quotationListResults = forminlineMapper.queryAll(qutationListVo);
        int count =forminlineMapper.queryCount(qutationListVo);
        allRecords.setDataList(quotationListResults);
        allRecords.setPageIndex(qutationListVo.getPageIndex());
        allRecords.setPageSize(qutationListVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<AllRecords> quotationListsearch(QuotationListVo quotationListVo) {
        AllRecords allRecords = new AllRecords();
        List<QuotationListResult>  quotationListResults = forminlineMapper.queryAllSheet(quotationListVo);
        int count =forminlineMapper.querySheetCount(quotationListVo);
        allRecords.setDataList(quotationListResults);
        allRecords.setPageIndex(quotationListVo.getPageIndex());
        allRecords.setPageSize(quotationListVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }


}
